
import { IPlugin, IConfig } from 'umi-types';
import path from 'path';
import pxToViewPort from 'postcss-px-to-viewport';
import pageRoutes from './router.config';

const { NODE_ENV } = process.env;

const plugins: IPlugin[] = [
  ['umi-plugin-react', {
    antd: true,
    dva: {
      hmr: true,
    },
    fastClick: true,
    dynamicImport: {
      loadingComponent: './components/PageLoading/index',
      webpackChunkName: true,
      level: 3,
    },
    library: 'react',
    metas: [{ charset: 'utf-8' }],
    title: {
      defaultTitle: 'lg',
    },
  }]
];

const uglifyJSOptions = NODE_ENV === 'production' ? {
  uglifyOptions: {
    // remove console.* except console.error
    compress: {
      drop_console: true,
      pure_funcs: ['console.error'],
    },
  },
} : {};

export default {
  base: '/',
  publicPath: '/dtwave_light/',
  history: 'hash',
  plugins,
  treeShaking: true,
  targets: {
    ie: 11,
    android: 5,
    ios: 7,
    chrome: 58,
  },
  devtool: false,
  routes: pageRoutes,
  proxy: {
    '/dtwave_light/api': {
      changeOrigin: true,
      // pathRewrite: { '^/dtwave_light/api/v1/dtwave_light_backend': '' },
      // target:  NODE_ENV === 'production' ? 'https://work.dtwave.com/' : 'http://118.31.14.51:8888/', // 这里后端需要提供
      target: 'http://118.31.14.51:8888/', // 这里后端需要提供
    },
  },
  lessLoaderOptions: {
    javascriptEnabled: true,
  },
  disableRedirectHoist: true,
  cssLoaderOptions: {
    modules: false,
  },
  manifest: {
    basePath: '/',
  },
  uglifyJSOptions,
  alias: {
    '@': path.resolve(__dirname, 'src'),
  },
  extraBabelPlugins: [
    ['import', { libraryName: 'antd-mobile', style: true }]  //按需加载antd-mobile样式文件
  ],
  extraPostCSSPlugins: [
    pxToViewPort({
      viewportWidth: 414,
      viewportHeight: 896,
      unitPrecision: 5,
      viewportUnit: 'vw',
      selectorBlackList: [],
      minPixelValue: 1,
      mediaQuery: false,
    }),
  ],
} as IConfig
